package com.haochang.chunkvod.view.vod.dialog;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseGridView;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.ScreenUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;
import com.haochang.chunkvod.tools.vod.other.GridViewUtil;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
 * 选择房间
 *     @author Yangjie
 *     className ChooseRoomDialog
 *     created at  2016/11/2  17:43
 */

public class ChooseRoomDialog extends Dialog
{
	public ChooseRoomDialog(Context context)
	{
		super(context);
	}

	public ChooseRoomDialog(Context context, int theme)
	{
		super(context, theme);
	}

	protected ChooseRoomDialog(Context context, boolean cancelable, OnCancelListener cancelListener)
	{
		super(context, cancelable, cancelListener);
	}

	public static class Builder
	{
		private Context context;
		private CharSequence title; //对话框标题
		private List<HostessInfo> dataList;
		private OnSingleChoosedListener mOnSelectListener; //对话框点击事件
		private QuickAdapter quickAdapter = null;

		public Builder(Context context)
		{
			this.context = context;
		}

		public Builder setTitle(int title)
		{
			this.title = context.getText(title);
			return this;
		}

		public Builder setTitle(CharSequence title)
		{
			this.title = title;
			return this;
		}

		public Builder setDataList(List<HostessInfo> dataList)
		{
			this.dataList = dataList;
			return this;
		}

		public List<HostessInfo> getDataList()
		{
			return dataList;
		}

		//设置单选List选项及事件，这些属性在之后的createDialog中用到，这里使用Android系统创建dialog的风格
		public Builder setSingleChooseItems(List<HostessInfo> dataList, final OnSingleChoosedListener listener)
		{
			this.dataList = dataList;
			this.mOnSelectListener = listener;
			for (HostessInfo info : dataList)
			{//初始化 itemTempType，itemTempRoom
				if (info.isShowBottom())
					itemTempType = info;
				for (HostessInfo info1 : info.getRooms())
					if (info1.isSelected())
						itemTempRoom = info1;
			}
			return this;
		}

		private HostessInfo itemTempType = null;
		private HostessInfo itemTempRoom = null;

		/**
		 * 根据规定的最大条数显示列表
		 *
		 * @param rows 最大显示条数
		 * @return ChooseActivityDialog
		 */
		public ChooseRoomDialog createDialog(int rows)
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final ChooseRoomDialog dialog = new ChooseRoomDialog(context, R.style.Alert_Dialog);
			final ViewGroup viewGroup = null;
			LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.inflate_choose_activity_dialog, viewGroup, false);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			BaseListView listView = (BaseListView) layout.findViewById(R.id.listView);
			if (rows == 0)
			{
				listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, DipPxConversion.dipToPx(context, 40 * dataList.size())));
			} else
			{
				//当数据条数小于设置最大显示条数时，高度设置为实际高度，当大于设置条数时，显示设置条数的高度
				listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, DipPxConversion.dipToPx(context, 41 * rows)));
			}
			quickAdapter = new QuickAdapter<HostessInfo>(context, R.layout.item_reserve_room_choose_room, dataList)
			{
				@Override
				protected void convert(final BaseAdapterHelper helper, final HostessInfo item)
				{
					final ImageView iv_arrow = helper.getView(R.id.iv_arrow);
					final LinearLayout linear_click = helper.getView(R.id.linear_click);
					final View viewSpace = helper.getView(R.id.viewSpace);
					final View viewSpaceTop = helper.getView(R.id.viewSpaceTop);
					final BaseGridView grid_view = helper.getView(R.id.grid_view);
					final ArrayList<HostessInfo> listRoom = item.getRooms();//可用房间list
					helper.setText(R.id.tv_title, item.getRoomType());
					iv_arrow.setImageResource(item.isShowBottom() ? R.drawable.up : R.drawable.down);
					grid_view.setVisibility(item.isShowBottom() ? View.VISIBLE : View.GONE);
					viewSpaceTop.setVisibility(!item.isShowBottom() && helper.getPosition() == dataList.size() - 1 ? View.GONE : View.VISIBLE);
					viewSpace.setVisibility(item.isShowBottom() && helper.getPosition() != dataList.size() - 1 ? View.VISIBLE : View.GONE);
					QuickAdapter gridAdapter = new QuickAdapter<HostessInfo>(context, R.layout.item_part_modify_grid_layout, listRoom)
					{
						@Override
						protected void convert(BaseAdapterHelper helper, final HostessInfo item)
						{
							helper.getView(R.id.img_del).setVisibility(View.GONE);
							helper.setText(R.id.tv_partName, item.getRoomNum());
							if (item.isSelected())//这个用于显示是否选中
							{
								helper.getView(R.id.tv_partName).setBackgroundResource(R.drawable.btn_red);
								((BaseTextView) helper.getView(R.id.tv_partName)).setTextColor(context.getResources().getColor(R.color.white));
							} else
							{
								helper.getView(R.id.tv_partName).setBackgroundResource(R.drawable.btn1);
								((BaseTextView) helper.getView(R.id.tv_partName)).setTextColor(context.getResources().getColor(R.color.color_282828));
							}

							helper.getView(R.id.tv_partName).setOnClickListener(new OnBaseClickListener()
							{
								@Override
								public void onBaseClick(View v)
								{
									if (null != itemTempRoom && !TextUtils.equals(itemTempRoom.getRoomNum(), item.getRoomNum()))
										itemTempRoom.setSelected(false);
									itemTempRoom = item;
									item.setSelected(!item.isSelected());
									quickAdapter.notifyDataSetChanged();
								}
							});
						}
					};
					grid_view.setAdapter(gridAdapter);
					GridViewUtil.setListViewHeightBasedOnChildren(context, grid_view);
					gridAdapter.notifyDataSetChanged();
					linear_click.setOnClickListener(new OnBaseClickListener()
					{
						@Override
						public void onBaseClick(View v)
						{
							if (itemTempType != null && !TextUtils.equals(item.getRoomType(), itemTempType.getRoomType()))
								itemTempType.setShowBottom(false);//将上一个显示gridview的item置为不显示GridView
							itemTempType = item;
							item.setShowBottom(!item.isShowBottom());
							quickAdapter.notifyDataSetChanged();
						}
					});
				}
			};
			listView.setAdapter(quickAdapter);
			BaseTextView tv_title = (BaseTextView) layout.findViewById(R.id.tv_title);
			tv_title.setText(title);
			layout.findViewById(R.id.tv_ensure).setOnClickListener(new OnBaseClickListener()
			{
				@Override
				public void onBaseClick(View v)
				{
					if (mOnSelectListener != null)
					{
						if (itemTempRoom != null)
						{
							mOnSelectListener.onSingleChoosed(itemTempRoom);
						}
						dialog.dismiss();
					}
				}
			});
			layout.findViewById(R.id.tv_cancel).setOnClickListener(new OnBaseClickListener()
			{
				@Override
				public void onBaseClick(View v)
				{
					dialog.dismiss();
				}
			});
			return dialog;
		}

		/**
		 * 显示window全宽，marginLeft，marginRight 20dp
		 * @param marginLeftAndmarginRight 左边距和右边距的值dp
		 * 如果小于0或者大于200则置为20
		 */
		public void showDialog(int rows, int marginLeftAndmarginRight)
		{
			ChooseRoomDialog dialog = createDialog(rows);
			dialog.show();
			Window window = dialog.getWindow();
			if (window != null)
			{
				WindowManager.LayoutParams layoutParams = window.getAttributes();
				layoutParams.width = ScreenUtils.getScreenWidth(context)
						- 2 * DipPxConversion.dip2px(context, marginLeftAndmarginRight >= 200 || marginLeftAndmarginRight < 0 ? 20 : marginLeftAndmarginRight);
				window.setAttributes(layoutParams);
				window.setGravity(Gravity.CENTER);
				window.setBackgroundDrawable(new ColorDrawable(0));
				dialog.setCancelable(true);
				dialog.setCanceledOnTouchOutside(false);
			}
		}
	}

	public interface OnSingleChoosedListener
	{
		void onSingleChoosed(HostessInfo info);
	}
}
